package com.huang.weic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.weic.bean.*;
import com.huang.weic.service.*;
import com.huang.weic.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/view")
public class viewController {

    @Autowired
    private viewService viewService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommService commService;
    @Autowired
    private ImagesService imagesService;
    @Autowired
    private videoService videoService;
    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;

    @GetMapping("/viewList/{openid}")
    public R getViewList(@PathVariable String openid){
        List<Object> list=viewService.selectViewList(openid);
        return R.ok().data("viewVoList",list);
    }

    //点赞
    @GetMapping("/love/{viewId}/{openid}")
    public R tolove(@PathVariable Integer viewId,@PathVariable String openid){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        User one = userService.getOne(wrapper);
        redisTemplate.opsForSet().add(String.valueOf(viewId),one.getNickname());
        return R.ok();
    }
    //取消
    @GetMapping("/nolove/{viewId}/{openid}")
    public R nolove(@PathVariable Integer viewId,@PathVariable String openid){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        User one = userService.getOne(wrapper);
        redisTemplate.opsForSet().remove(String.valueOf(viewId),one.getNickname());
        return R.ok();
    }



    //发表动态
    @PostMapping("/addView")
    public R addView(@RequestBody view view){
         viewService.save(view);
        Integer viewId = view.getId();
        return R.ok().data("viewId",viewId);
    }

    //删除动态
    @DeleteMapping("/deleteView/{viewid}")
    public R deleteView(@PathVariable Integer viewid){
        //删除动态
        viewService.removeById(viewid);

        try {
            //删除评论
            QueryWrapper<comm> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("view_id",viewid);
            commService.remove(wrapper1);
            //删除 图片
            QueryWrapper<imges> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("view_id",viewid);
            imagesService.remove(wrapper2);
            //删除视频
            QueryWrapper<videos> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("view_id",viewid);
            videoService.remove(wrapper3);
            //删除 点赞
            redisTemplate.opsForSet().remove(String.valueOf(viewid));
        }catch (Exception e){
            e.printStackTrace();
        }

        return R.ok();
    }
}
